#!/usr/bin/env python
# -*- coding: utf-8 -*-
#  deploy_dev.py
#  description

#  Created by zhouzongli633 on 2020/7/23 1:41 下午.
#  Copyright ©Intellectus All rights reserved.

import datetime
import os
import select
import shutil
import sys
import socket
import paramiko
from paramiko.py3compat import u
from NICU2HOME import settings


config_dev = {
    "host": ('40.113.230.237', 22),
    "username": "nicu2home",
    "password": "nicu2020",
    "remote_path": "/home/nicu2home/nicu2home_django/working/",
}
config_prd = {
    "host": ('13.67.221.124', 22),
    "username": "nicu2home",
    "password": "NicuAzure2018",
    "remote_path": "/home/nicu2home/nicu2home_django/working/",
}

if __name__ == "__main__":
    try:
        # 打包
        config = {}
        if settings.Environment == 'dev':
            config = config_dev
        if settings.Environment == 'prd':
            config = config_prd
        if settings.Environment == 'loc':
            print("ERROR: loc")
            raise None
        base_name = os.path.dirname(os.path.realpath(__file__))
        tmp_folder_name = "tmp-%s" % datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
        # code_folder = '/Users/zhouzongli/nicu2home-plus-django/nicu2home-plus-django/NICU2HOME'
        code_folder = r'C:\\new_company_code\\nicu2home-plus-django\\nicu2home-plus-django\\NICU2HOME'
        save_folder = r'C:\\Users\\Dell\\Desktop\\'
        file_name = "nicu2home_{}_{}".format(settings.Environment, datetime.datetime.now().strftime("%Y%m%d%H%M"))
        shutil.make_archive(save_folder + file_name, 'zip', root_dir=code_folder)
        file_name = file_name + '.zip'
        package_path = save_folder + file_name
        print("package finish:{}".format(package_path))

        # 传输
        remote_path = config['remote_path']
        tran = paramiko.Transport(config['host'])
        tran.connect(username=config['username'], password=config['password'])
        p = paramiko.SFTPClient.from_transport(tran)
        upload_path = remote_path + file_name
        p.put(package_path, upload_path)  # 上传文件到远程机
        p.close()
        print("trans finish:{}".format(upload_path))

        # 部署
        chan = tran.open_session()
        chan.get_pty()
        chan.invoke_shell()
        while True:
            readable, writeable, error = select.select([chan, sys.stdin, ], [], [], 1)
            if chan in readable:
                try:
                    x = u(chan.recv(1024))
                    if len(x) == 0:
                        print('\r\n*** EOF\r\n')
                        break
                    sys.stdout.write(x)
                    sys.stdout.flush()
                except socket.timeout:
                    pass
            if sys.stdin in readable:
                inp = sys.stdin.readline()
                chan.sendall(inp)
        chan.close()
        tran.close()

    except Exception as e:
        print(e)
